Fix phpstan/phpstan#9844: Calling a static method over non-generic class-string results in ErrorType#5418
Closed
phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
Closed
Conversation
…mixed instead of ErrorType - When calling a static method on a non-generic class-string variable, return mixed instead of ErrorType - The fix only applies to class-string types, not to named class or typed object expressions - New regression test in tests/PHPStan/Analyser/nsrt/bug-9844.php - Root cause: class-string converts to ObjectWithoutClassType which has hasMethod() = MAYBE, causing filterTypeWithMethod to bail out and fall back to ErrorType
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
Keep the ErrorType for $class::foo() and instead, report an error to be consistent with
/**
* @param object $class
*/
public function sayHello2(object $class): void
{
\PHPStan\dumpType($class::foo());
}
which report
Call to an undefined static method object::foo().
Keep ErrorType for `$class::foo()` on non-generic class-string, consistent with how `object::foo()` reports an error. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
|
Already handled — |
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
Add a RuleTest for
/**
* @param class-string $class
*/
public function sayHello(string $class): void
{
$class::foo();
}
/**
* @param object $class
*/
public function sayHello2(object $class): void
{
$class::foo();
}
and ensure BOTH report a static.notFound error
Static method calls on bare class-string (e.g. $class::foo()) now report "Call to an undefined static method class-string::foo()" instead of silently returning no errors. This is consistent with how object type static calls already report the error. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
|
All four background tasks (2x tests, 2x phpstan) completed successfully. Everything is green and pushed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When calling a static method on a non-generic
class-stringvariable (e.g.$class::foo()where$classisclass-string), PHPStan returned*ERROR*type instead ofmixed. This is incorrect becauseclass-stringrepresents any class, and the method could exist on whatever class it turns out to be at runtime.Changes
src/Analyser/ExprHandler/StaticCallHandler.phpto track when the static call target comes from a class-string type, and returnmixedinstead ofErrorTypewhen the method might exist on the typetests/PHPStan/Analyser/nsrt/bug-9844.phpRoot cause
The flow was:
class-stringgets converted toObjectWithoutClassTypeviagetObjectTypeOrClassStringObjectType()ObjectWithoutClassType::hasMethod()returnsTrinaryLogic::MAYBE(the method might exist)MutatingScope::filterTypeWithMethod()requireshasMethod()->yes(), so it returnsnullMethodCallReturnTypeHelper::methodCallReturnType()returnsnullStaticCallHandlerfalls back toErrorTypeThe fix detects when the caller is a class-string type and the target type says the method might exist (MAYBE), returning
mixedinstead ofErrorType. This is consistent with how calling methods onmixedreturnsmixed.Test
Added
tests/PHPStan/Analyser/nsrt/bug-9844.phpwhich asserts that$class::foo()where$class: class-stringreturnsmixed.Fixes phpstan/phpstan#9844